package ee.ut.aa.neuraltic.genetic;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ee.ut.aa.neuraltic.neural.Network;
import ee.ut.aa.neuraltic.neural.TicNetwork;

public class Brain {

	private static Logger log = Logger.getLogger( Brain.class );

	public static int POPULATION = 20;
	public static int MAXPOP = POPULATION * POPULATION + POPULATION;
	public static double MUTATION = 0.95;

	private Knowledge knowledge = new Knowledge();
	private List<Network> population = new ArrayList<Network>( MAXPOP );

	public void initPopulation() {

		log.debug( "Initializing population." );
		
		for( int i = 0; i < POPULATION; i++ )
			population.add( new TicNetwork() );
	}

	public void iteration() {

		ArrayList<Network> newPop = new ArrayList<Network>( MAXPOP );

		Network child;

		population = knowledge.naturalSelection( population );

		for( Network father : population ) {
			for( Network mother : population ) {

				//father = knowledge.chooseMate( population );
				mother = knowledge.chooseMate( population );

				child = knowledge.mate( father, mother );

				if( Math.random() > MUTATION )
					knowledge.mutate( child );

				newPop.add( child );
			}
		}

		population.addAll( newPop );
	}

	public Knowledge getKnowledge() {
		return knowledge;
	}

	public void setKnowledge( Knowledge knowledge ) {
		this.knowledge = knowledge;
	}

	public List<Network> getPopulation() {
		return population;
	}

	public void setPopulation( ArrayList<Network> population ) {
		this.population = population;
	}
}
